﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class CustomerDayOnly : Form
    {
        private DataTable table;
        public CustomerDayOnly()
        {
            InitializeComponent();
        }

        private Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        private DataGridViewImageCell loadImage(string url)
        {
            try
            {
                Image image = Image.FromFile(url);
                DataGridViewImageCell imageCell = new DataGridViewImageCell();
                Image imagedis = resizeImage(image, new Size(150, 150));
                imageCell.Value = imagedis;
                image.Dispose();
                return imageCell;
            }
            catch
            {
                return new DataGridViewImageCell();
            }
        }

        private void init(String name)
        {
            PurchaseDao dao = new PurchaseDao();
            table = dao.GetCustomerDayOnly(name);
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = row["ID"].ToString();
                    dtgCustomers.Rows[i].Cells[1].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[2] = loadImage(row["PICTURE"].ToString());
                    dtgCustomers.Rows[i].Cells[3].Value = row["NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[4].Value = row["NOTE"].ToString();
                    dtgCustomers.Rows[i].Cells[5].Value = DateTime.Parse(row["CREATE_DATE"].ToString()).ToString("dd/MM/yyyy");
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            init(txtName.Text.Trim());
        }

        private void dtgCustomers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string name = dtgCustomers.Rows[e.RowIndex].Cells[3].Value.ToString();

                if (e.RowIndex >= 0 && e.ColumnIndex == 2)
                {
                    int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[0].Value.ToString());
                    PurchaseDay p = new PurchaseDay(id);
                    p.ShowDialog();
                    init(txtName.Text.Trim());
                }
                else if (e.RowIndex >= 0 && e.ColumnIndex == 3)
                {
                    int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[0].Value.ToString());
                    UpdateCustomer p = new UpdateCustomer(id);
                    p.ShowDialog();
                    init(txtName.Text.Trim());

                }
                else if (e.RowIndex >= 0 && e.ColumnIndex == 4)
                {
                    int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[0].Value.ToString());
                    CustomerDetail p = new CustomerDetail(id);
                    p.ShowDialog();
                }
                else if (e.RowIndex >= 0 && e.ColumnIndex == 5)
                {
                    int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[0].Value.ToString());
                    Purchase p = new Purchase(id);
                    p.ShowDialog();
                    init(txtName.Text.Trim());
                }
            }
        }
    }
}
